﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace Abs.App.Booking
{
    
    public partial class Make_Appointment : System.Web.UI.Page
    {
        
        private string adminMaster = "~/MasterPages/AdminMaster.Master";
        private string userMaster = "~/MasterPages/UserMaster.Master";
        

        protected void Page_PreInit(object sender, EventArgs e)
        {
            //Determine which master page to use based on the role of the account (normal user/ admin)
            if (Session["Role"] != null && ((int)Session["Role"] == (int)Abs.Core.Models.User.RoleEnum.Admin))
            {
                MasterPageFile = adminMaster;
            }
            else
            {
                MasterPageFile = userMaster;
            }
            
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Email"] == null)
            {
                Response.Redirect("~/User/Unauthorized_Access.aspx");
            }
                

            var roomManager = Abs.Core.Management.Manage.Room;
            if (!IsPostBack)
            {
                var roomList = roomManager.SelectAllRoom();

                foreach (var room in roomList)
                {
                    ListItem item = new ListItem();
                    item.Text = room.Name;
                    item.Value = room.RoomId.ToString();
                    Drop_List_Room.Items.Add(item);
                }
            }
            
            
        }

        protected void Review_Entry(object sender, EventArgs e)
        {
            Text_CDate.Text = datepicker.Text;
            Text_Ccomments.Text = Text_Comments.Text;
            Text_CTime.Text = Drop_List_Time.SelectedItem.Text;
            Text_CETime.Text = Drop_List_Etime.SelectedItem.Text;
            Text_CRoom.Text = Drop_List_Room.SelectedItem.Text;
            Label2.Visible = false;
        }


        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (!string.IsNullOrEmpty(Text_CDate.Text))
            {
                //make sure that start time comes before end time
                if (!(DateTime.Compare(DateTime.Parse(Drop_List_Time.Text), DateTime.Parse(Drop_List_Etime.Text)) < 0))
                {
                    Label2.Visible = true;
                    return;
                }
                
                var roomId = int.Parse(Drop_List_Room.SelectedValue);
                var appointmentManager = Abs.Core.Management.Manage.Appointment;
                var roomManager = Abs.Core.Management.Manage.Room;
                
                int dayId = (int)DateTime.Parse(datepicker.Text).DayOfWeek;
                int startTime = DateTime.Parse(Text_CTime.Text).Hour;
                int endTime = DateTime.Parse(Text_CETime.Text).Hour;
                var schedule = roomManager.SelectRoomSchedule(roomId, dayId);
                
                if (schedule == null)
                {
                    //no slots on that day
                    Response.Redirect("~/Booking/Booking_Failed.aspx");
                }
                if (schedule.StartTime.Hour > startTime || schedule.EndTime.Hour < endTime)
                {
                    //Too early or too late
                    Response.Redirect("~/Booking/Booking_Failed.aspx");
                }      
                if(appointmentManager.CreateAppointment(roomId,Session["Email"].ToString(),
                    Text_CDate.Text + " " + Text_CTime.Text, Text_CDate.Text + " " + Text_CETime.Text,Text_Ccomments.Text))
                {
                    Response.Redirect("~/Booking/Booking_Success.aspx");
                }
                Response.Redirect("~/Booking/Booking_Failed.aspx");
            }
            
        }

        protected void datepicker_TextChanged(object sender, EventArgs e)
        {
            
            Label1.Visible = false;
        }

        

        
    }
}